package middle;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class Solution_227 {

    public static void main(String[] args) {
//        Solution_227 model = new Solution_227();
//        System.out.println(model.calculate("3+2 * 2"));
//        System.out.println(model.calculate(" 3/2 "));
//        System.out.println(model.calculate(" 3+5 / 2 "));
//        System.out.println(model.calculate("3"));
//        System.out.println(model.calculate("2147483647 * 3 / 3"));
        List<Integer> list = new ArrayList<>();
        list.add(3);
        list.add(4);
        list.add(1);
        list.add(2);
        list.sort((e1, e2) -> e1 < e2 ? -1 : e1 == e2 ? 0 : 1);
        System.out.println(list);
    }

    public int calculate(String s) {
        s+=" + 0 +";
        Integer one = null, two = null;
        Character one_c = null, two_c = null;
        char[] chars = s.toCharArray();
        StringBuilder buffer = new StringBuilder();
        for (char c : chars) {
            if (c == ' ') {
                continue;
            }
            if (c == '+' || c == '-' || c == '*' || c == '/') {
                if (buffer.length() > 0) {
                    if (one == null) {
                        one = Integer.valueOf(buffer.toString());
                        one_c = c;
                    } else if (two == null) {
                        two = Integer.valueOf(buffer.toString());
                        two_c = c;
                    } else {
                        if ((one_c == '+' || one_c == '-') && (two_c == '*' || two_c == '/')) {
                            two = cal(two, Integer.parseInt(buffer.toString()), two_c);
                            two_c = c;
                        } else {
                            one = cal(one, two, one_c);
                            two = Integer.valueOf(buffer.toString());
                            one_c = two_c;
                            two_c = c;
                        }
                    }
                    buffer.delete(0, buffer.length());
                }
            } else {
                buffer.append(c);
            }
        }
        return one;
    }

    private int cal(int one, int two, char c) {
        switch (c) {
            case '*':
                return one * two;
            case '/':
                return one / two;
            case '+':
                return one + two;
            case '-':
                return one - two;
            default:
                return 0;
        }
    }
}
